#!/usr/bin/env python
# -*- coding: UTF-8 -*-
'''
@Project ：pythonProject2 
@File    ：heroImage.py
@Author  ：beibei
@Date    ：2024/3/26 2:54 PM 
'''

import requests
import os


def fetch_hero_list():
    url = 'https://game.gtimg.cn/images/lol/act/img/js/heroList/hero_list.js?v=20'
    response = requests.get(url)
    if response.status_code == 200:
        data = response.json()
        hero_list = data.get('hero', [])

        for hero in hero_list:
            hero_id = hero.get('heroId')
            hero_name = hero.get('name')
            return hero_id, hero_name
def fetch_hero_skins(hero_id):
    url = f'https://game.gtimg.cn/images/lol/act/img/js/hero/{hero_id}.js'
    response = requests.get(url)
    if response.status_code == 200:
        data = response.json()
        hero_skins = data.get('skins', [])

        for skin in hero_skins:
            skin_id = skin.get('skinId')
            skin_name = skin.get('name')
            return skin_id, skin_name

def fetch_hero_images(skin_id):
    url = f'https://game.gtimg.cn/images/lol/act/img/skin/big{skin_id}.jpg'
    response = requests.get(url)
    if response.status_code == 200:
        img_data = response.content
        save_path = f"big{skin_id}.jpg"

        with open(save_path,'wb') as f:
            f.write(img_data)
        print(f'图片已保存到{save_path}')
    else:
        print('图片状态码不是200，跳过保存')





# def save_skin_image(skin_id, skin_name, hero_name):
#     url = f'https://game.gtimg.cn/images/lol/act/img/skin/big{skin_id}.jpg'
#     img_data = requests.get(url).content
#     save_path = f'lol_skins/{hero_name}'
#     os.makedirs(save_path, exist_ok=True)
#     with open(f'{save_path}/{skin_name}.jpg', 'wb') as handler:
#         handler.write(img_data)
#
# def main():
#     heroes = fetch_hero_list()
#     for hero in heroes:
#         hero_id = hero['heroId']
#         hero_name = hero['name']
#         skins = fetch_hero_skins(hero_id)
#         for skin in skins:
#             if skin['name'] != 'default':
#                 save_skin_image(skin['skinId'], skin['name'], hero_name)
#
# if __name__ == '__main__':
#     main()
